package com.loren.demo33.config;

import com.loren.demo33.event.BizEventListener;
import com.loren.demo33.event.DefaultEventEngine;
import com.loren.demo33.event.EventEngine;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class EventConfig {

    private static final Executor EXECUTOR = new ThreadPoolExecutor(5, 50, 20L, TimeUnit.SECONDS,
            new LinkedBlockingQueue<>(500), new CustomizableThreadFactory("event-engine-pool-"));

    @Resource
    private List<BizEventListener> bizEventListeners;

    @Bean
    public EventEngine eventEngine() {
        DefaultEventEngine eventEngine = new DefaultEventEngine(this.bizEventListeners);
        eventEngine.setExecutor(EXECUTOR);
        return eventEngine;
    }
}